/**
 * @file test_mpu6050_driver.c
 * @author liufeng (liufeng@injoinic.com)
 * @brief 测试mpu6050 driver层代码
 * @version 0.1
 * @date 2024-11-20
 *
 * @copyright Copyright (c) 2024  injoinic
 *
 */

#include "bsp_mpu6050_driver.h"
#include "hal_iic.h"
#include "main.h"
#include "stdio.h"

#if 0

mpu6050_driver_typedef mmp;

// 测试条件
// 1：裸机环境，主要是测试硬件IIC和外部中断，以及mpu6050的读写
// 2：请注释掉mpu6050_driver.c中的vTask_delay换成LL_mDelay();以及发送任务通知的函数
// 3：接线说明：

void test_mpu6050_driver()
{
    mpu6050_construct(&mmp, &i2c1_handle);
    mmp.pf_init(&mmp);

    float ax = 0, ay = 0, az = 0;
    float gx = 0, gy = 0, gz = 0;
    float temperature = 0;
    uint8_t* buffer_addr = 0; // 待处理的缓冲区地址

    while (1) {
        buffer_addr = mmp.mpu6050_data_buff.pf_read_addr(&mmp.mpu6050_data_buff);
        if (buffer_addr != NULL) {
            // 数据处理
            int16_t accel_x_raw = ((int16_t)buffer_addr[0] << 8) | buffer_addr[1];
            int16_t accel_y_raw = ((int16_t)buffer_addr[2] << 8) | buffer_addr[3];
            int16_t accel_z_raw = ((int16_t)buffer_addr[4] << 8) | buffer_addr[5];
            ax = accel_x_raw / 16384.0;
            ay = accel_y_raw / 16384.0;
            az = accel_z_raw / 16384.0;

            int16_t raw = ((int16_t)buffer_addr[6] << 8) | buffer_addr[7];
            temperature = 36.53f + (raw / 340.f);

            int16_t gyro_x_raw = ((int16_t)buffer_addr[8] << 8) | buffer_addr[9];
            int16_t gyro_y_raw = ((int16_t)buffer_addr[10] << 8) | buffer_addr[11];
            int16_t gyro_z_raw = ((int16_t)buffer_addr[12] << 8) | buffer_addr[13];
            gx = gyro_x_raw / 131.0;
            gy = gyro_y_raw / 131.0;
            gz = gyro_z_raw / 131.0;

            // 打印结果
            printf("temprature is %f\r\n", temperature);
            printf("ax: %f, ay: %f, az: %f\r\n", ax, ay, az);
            printf("gx: %f, gy: %f, gz: %f\r\n\r\n", gx, gy, gz);

            // 移动缓冲区的头指针
            mmp.mpu6050_data_buff.pf_move_head(&mmp.mpu6050_data_buff);
        }
    }
}

void DMA1_Stream0_IRQHandler(void)
{
    if (LL_DMA_IsEnabledIT_TC(i2c1_handle.dmarx, i2c1_handle.dmarx_stream) &&
        LL_DMA_IsActiveFlag_TC0(i2c1_handle.dmarx)) {
        LL_DMA_ClearFlag_TC0(i2c1_handle.dmarx);

        LL_I2C_DisableDMAReq_RX(i2c1_handle.Instance);
        LL_I2C_GenerateStopCondition(i2c1_handle.Instance);

        mmp.pfdma_completed_callback(&mmp);
    }
}

void EXTI9_5_IRQHandler(void)
{
    if (LL_EXTI_IsActiveFlag_0_31(LL_EXTI_LINE_8) != RESET) {
        LL_EXTI_ClearFlag_0_31(LL_EXTI_LINE_8);

        mmp.pfint_interrupt_callback(&mmp);
    }
}

#endif
